package com.lu.pojo.thread.syn;

/**
 * @author luzhenfang
 * @date 2020 05 24
 * @description 多线程买票， 不安全。
 */
public class UnsafeBuyTicket {
    public static void main(String[] args) {
        BuyTicket buyTicket = new BuyTicket();
        new Thread(buyTicket,"我").start();
        new Thread(buyTicket,"你").start();
        new Thread(buyTicket,"黄牛").start();
    }
}

class  BuyTicket implements Runnable{
    private int ticketNums =10;
    private boolean flag =true;

    @Override
    public void run() {
        // 买票
        while (flag){
            buy();
        }

    }
    private void buy(){
        if (ticketNums<=0){
            flag=false;
            return;
        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 买票
        System.out.println(Thread.currentThread().getName()+"拿到"+ticketNums--);
    }
}
